package com.ttl.bc.ap.purchaseorder.db;

import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;


import com.ttl.bc.ap.purchaseorder.action.BCPurchaseOrderForm;
import com.ttl.bc.ap.purchaseorder.action.BCPurchaseOrderFullForm;
import com.ttl.bc.ap.purchaseorder.action.POFindForm;
import com.ttl.bc.ap.purchaseorder.bean.BCPurchaseOrderBean;
import com.ttl.bc.ap.purchaseorder.bean.BCPurchaseOrderShowBean;
import com.ttl.bc.ap.purchaseorder.bean.BCPurchaseOrderSubBean;
import com.ttl.bc.ap.purchaseorder.bean.BCPurchaseOrderSubShow2Bean;
import com.ttl.bc.ap.purchaseorder.bean.BCPurchaseOrderSubShowBean;
import com.ttl.bc.ap.stkrequest.BCStkRequestBO;
import com.ttl.bc.ap.stkrequest.BCStkRequestBean;
import com.ttl.bc.ap.stkrequest.BCStkRequestDAO;
import com.ttl.bc.ap.stkrequest.BCStkRequestForm;
import com.ttl.bc.ap.stkrequest.BCStkRequestSubBean;
import com.ttl.bc.ap.stkrequest.BCStkRequestSubShowBean;

import com.ttl.bc.ar.reqconfirm.bean.BCReqConfirmBean;
import com.ttl.bc.ar.reqconfirm.bean.BCReqConfirmShowBean;
import com.ttl.bc.ar.reqconfirm.bean.BCReqConfirmSubBean;
import com.ttl.bc.ar.reqconfirm.bean.BCReqConfirmSubShowBean;

import com.ttl.bc.ar.reqconfirm.db.BCReqConfirmDAO;
import com.ttl.bc.ic.item.BCItemBO;
import com.ttl.bc.ic.item.BCItemBean;

import com.ttl.bc.master.itemunit.BCItemUnitBean;

import com.ttl.common.util.CommonUtils;
import com.ttl.common.util.Page;

public class BCPurchaseOrderBO {

	
	@SuppressWarnings("unchecked")
	public ArrayList findOLD(ActionForm form, HttpServletRequest request) {
		
		BCPurchaseOrderForm poForm =(BCPurchaseOrderForm)form; 
								
		String hql = " FROM BCReqConfirmBean s  " +
					 " WHERE  " +
					 " s.docNo in ( '"+poForm.getItemCode1()+"', "+
					 " '"+poForm.getItemCode2()+"', "+
					 " '"+poForm.getItemCode3()+"', "+
					 " '"+poForm.getItemCode4()+"') and "+
					 " s.billStatus=0 ";								
		
		BCPurchaseOrderDAO dao = new BCPurchaseOrderDAO();
		ArrayList results = (ArrayList)dao.find(hql);
				
		ArrayList al = new ArrayList();
		
		if(results!=null && results.size()>0) {
			
			for(int i=0; i<results.size(); i++) {
				BCReqConfirmBean f = (BCReqConfirmBean)results.get(i);
				
		    	Iterator it = f.getBcReqConfirmSubs().iterator();
		    	
		        while(it.hasNext()) {
		        	BCReqConfirmSubBean sub = (BCReqConfirmSubBean)it.next();
		        	
		        	BCPurchaseOrderShowBean bean = new BCPurchaseOrderShowBean();
		        	bean.setItemCode(sub.getItemCode());
		        	bean.setItemName(sub.getItemName());
		        	
		        	hql = "FROM BCStkRequestBean WHERE docNo='"+f.getDocNo()+"'";
		        	BCStkRequestDAO dao2 = new BCStkRequestDAO();
		        	ArrayList al2 = (ArrayList)dao2.find(hql);
		        	//	System.out.println("size of BCStkRequestBean: "+al2.size());
		        	
		        	for(int j=0; j<al2.size(); j++) {
		        		//System.out.println("j="+j);
		        		
		        		BCStkRequestBean stk = (BCStkRequestBean)al2.get(j);
		        		
				    	Iterator it2 = stk.getBcStkRequestSubs().iterator();
				    	@SuppressWarnings("unused")
						int x =0;
				        while(it2.hasNext()) {
				        	BCStkRequestSubBean sub2 = (BCStkRequestSubBean)it2.next();
				        	if(sub2.getItemCode().equals(sub.getItemCode()) && 
				        			sub2.getItemName().equals(sub.getItemName() )) {
				        		bean.setUnitName(sub2.getIttl_unitName());
				        	}				        	
				        }
		        	}
						
		        	bean.setUnitCode(sub.getUnitCode());
		        	bean.setQty(""+sub.getQty());
		        	bean.setStkReqNo(sub.getStkRequestNo());	
		        	bean.setConfirmNo(f.getDocNo());
		        	
		        	al.add(bean);		        		
		        }
			}
		}
		
		return al;	
	}

	@SuppressWarnings("unchecked")
	public BCPurchaseOrderFullForm setPOForm(ArrayList results) {				
		
		BCPurchaseOrderFullForm form = new BCPurchaseOrderFullForm();
		
		for(int i=0; i<results.size(); i++) {
			BCPurchaseOrderShowBean bean = (BCPurchaseOrderShowBean)results.get(i);
			switch (i+1) {
			case 1:
					form.setItemCode1(bean.getItemCode());
					form.setItemName1(bean.getItemName());
					form.setUnitCode1(bean.getUnitCode());	
					form.setUnitName1(bean.getUnitName());
				
					form.setStkReqNo1(bean.getStkReqNo());
					form.setConfirmNo1(bean.getConfirmNo());
					
					break;
			case 2:
				form.setItemCode2(bean.getItemCode());
				form.setItemName2(bean.getItemName());
				form.setUnitCode2(bean.getUnitCode());
				form.setUnitName2(bean.getUnitName());
				
				form.setStkReqNo2(bean.getStkReqNo());
				form.setConfirmNo2(bean.getConfirmNo());								
				break;
			case 3:
				form.setItemCode3(bean.getItemCode());
				form.setItemName3(bean.getItemName());
				form.setUnitCode3(bean.getUnitCode());
				form.setUnitName3(bean.getUnitName());
				
				form.setStkReqNo3(bean.getStkReqNo());
				form.setConfirmNo3(bean.getConfirmNo());								
				break;
			case 4:
				form.setItemCode4(bean.getItemCode());
				form.setItemName4(bean.getItemName());
				form.setUnitCode4(bean.getUnitCode());
				form.setUnitName4(bean.getUnitName());
				
				form.setStkReqNo4(bean.getStkReqNo());
				form.setConfirmNo4(bean.getConfirmNo());								
				break;
			case 5:
				form.setItemCode5(bean.getItemCode());
				form.setItemName5(bean.getItemName());
				form.setUnitCode5(bean.getUnitCode());
				form.setUnitName5(bean.getUnitName());
				
				form.setStkReqNo5(bean.getStkReqNo());
				form.setConfirmNo5(bean.getConfirmNo());								
				break;
			case 6:
				form.setItemCode6(bean.getItemCode());
				form.setItemName6(bean.getItemName());
				form.setUnitCode6(bean.getUnitCode());
				form.setUnitName6(bean.getUnitName());
				
				form.setStkReqNo6(bean.getStkReqNo());
				form.setConfirmNo6(bean.getConfirmNo());								
				break;
			case 7:
				form.setItemCode7(bean.getItemCode());
				form.setItemName7(bean.getItemName());
				form.setUnitCode7(bean.getUnitCode());
				form.setUnitName7(bean.getUnitName());
				
				form.setStkReqNo7(bean.getStkReqNo());
				form.setConfirmNo7(bean.getConfirmNo());								
				break;
			case 8:
				form.setItemCode8(bean.getItemCode());
				form.setItemName8(bean.getItemName());
				form.setUnitCode8(bean.getUnitCode());
				form.setUnitName8(bean.getUnitName());
				
				form.setStkReqNo8(bean.getStkReqNo());
				form.setConfirmNo8(bean.getConfirmNo());								
				break;
			case 9:
				form.setItemCode9(bean.getItemCode());
				form.setItemName9(bean.getItemName());
				form.setUnitCode9(bean.getUnitCode());
				form.setUnitName9(bean.getUnitName());
				
				form.setStkReqNo9(bean.getStkReqNo());
				form.setConfirmNo9(bean.getConfirmNo());								
				break;
			case 10:
				form.setItemCode10(bean.getItemCode());
				form.setItemName10(bean.getItemName());
				form.setUnitCode10(bean.getUnitCode());
				form.setUnitName10(bean.getUnitName());
				
				form.setStkReqNo10(bean.getStkReqNo());
				form.setConfirmNo10(bean.getConfirmNo());								
				break;
			case 11:
				form.setItemCode11(bean.getItemCode());
				form.setItemName11(bean.getItemName());
				form.setUnitCode11(bean.getUnitCode());
				form.setUnitName11(bean.getUnitName());
				
				form.setStkReqNo11(bean.getStkReqNo());
				form.setConfirmNo11(bean.getConfirmNo());								
				break;
			case 12:
				form.setItemCode12(bean.getItemCode());
				form.setItemName12(bean.getItemName());
				form.setUnitCode12(bean.getUnitCode());
				form.setUnitName12(bean.getUnitName());
				
				form.setStkReqNo12(bean.getStkReqNo());
				form.setConfirmNo12(bean.getConfirmNo());								
				break;
			case 13:
				form.setItemCode13(bean.getItemCode());
				form.setItemName13(bean.getItemName());
				form.setUnitCode13(bean.getUnitCode());
				form.setUnitName13(bean.getUnitName());
				
				form.setStkReqNo13(bean.getStkReqNo());
				form.setConfirmNo13(bean.getConfirmNo());								
				break;
			case 14:
				form.setItemCode14(bean.getItemCode());
				form.setItemName14(bean.getItemName());
				form.setUnitCode14(bean.getUnitCode());
				form.setUnitName14(bean.getUnitName());
				
				form.setStkReqNo14(bean.getStkReqNo());
				form.setConfirmNo14(bean.getConfirmNo());								
				break;
			case 15:
				form.setItemCode15(bean.getItemCode());
				form.setItemName15(bean.getItemName());
				form.setUnitCode15(bean.getUnitCode());
				form.setUnitName15(bean.getUnitName());
				
				form.setStkReqNo15(bean.getStkReqNo());
				form.setConfirmNo15(bean.getConfirmNo());								
				break;				
			default:
				break;
			}
			
		}
		
		return form;
	}

	@SuppressWarnings("unchecked")
	public boolean add(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		//ถ้ามาจากหน้า : สร้าง PO จาก PO จะได้ค่า changePO= 'YES' ซึ่ง add ข้อมูลลง bcpurchaseorder,bcpurchaseorderSubเท่านั้น
		//ถ้ามาจากหน้า : สร้าง PO จากใบ พ1 :changePO=null
		String changePO = (String)request.getParameter("changePO");  
		
		// ถ้าเป็นการสร้าง PO จาก ใบ PO จะไม่ต้องกลับไป update ใน Table :BCStkRequest, BCStkRequestSub , BCReqConfirm, BCReqConfirmSub
		
		if(changePO==null || !changePO.equalsIgnoreCase("YES")) {
			System.out.println("add po แบบ ดึงจาก พ.1");
			// ###########   Start : Update Table BCStkRequest, BCStkRequestSub  #########
			String rowOrderLineItem[] = new String[4]; 
			rowOrderLineItem[0] = request.getParameter("rowOrder1");
			rowOrderLineItem[1] = request.getParameter("rowOrder2");
			rowOrderLineItem[2] = request.getParameter("rowOrder3");
			rowOrderLineItem[3] = request.getParameter("rowOrder4");
			
			String stkReqDocNo = request.getParameter("docNo");
			request.setAttribute("docNoFind", stkReqDocNo);
			//TODO ทำ update : ittl_status = 1
			
			BCStkRequestForm reqForm = BCStkRequestBO.setForm(request);				
			BCStkRequestBean  stkReq = BCStkRequestBO.setRequestObject(reqForm);
			stkReq.setIsConfirm(1);
			stkReq.setIsCancel(0);
			stkReq.setBillStatus(1);
			stkReq.setIttl_status("1");//ออกใบ PO แล้วจะเป็น 1
			stkReq.setIttl_userLevel((Integer)request.getSession().getAttribute("userLevel"));
			
			Iterator it = stkReq.getBcStkRequestSubs().iterator();
			while(it.hasNext()) {
				BCStkRequestSubBean sub = (BCStkRequestSubBean)it.next();
				sub.setIsCancel(0);
				sub.setIttl_status("1");
			}						
			
			BCStkRequestDAO dao = new BCStkRequestDAO();
//			dao.updateStatus(stkReqDocNo, rowOrderLineItem, "1");//ittl_status=1=>สร้าง po แล้ว
			dao.updateAfterCreatePO(stkReq,rowOrderLineItem, "1");
			
			String user 	 = (String)request.getSession().getAttribute("userName");
			System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestDAO #mode :updateStatus");
			// ###########   END : Update Table BCStkRequest, BCStkRequestSub  #########
					
			// ###########   Start : Update Table BCReqConfirm, BCReqConfirmSub  #########		
//			BCReqConfirmBean reqConfirm = BCStkRequestBO.setReqConfirm(stkReq);
//			reqConfirm.setIsConfirm(0);
//			reqConfirm.setBillStatus(1);
//			reqConfirm.setIsCancel(0);
//			reqConfirm.setIttl_status("1");
//			reqConfirm.setIttl_userLevel((Integer)request.getSession().getAttribute("userLevel"));
//			
//			it =reqConfirm.getBcReqConfirmSubs().iterator();
//			while(it.hasNext()) {
//				BCReqConfirmSubBean sub = (BCReqConfirmSubBean)it.next();
//				sub.setIttl_status("1");
//			}			
//			BCReqConfirmDAO dao2 = new BCReqConfirmDAO();
//			dao2.updateStatus(stkReqDocNo, rowOrderLineItem, "1");//ittl_status=1=>สร้าง po แล้ว
//					
//			System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCReqConfirmDAO #mode :update");
			// ###########   END : Update Table BCReqConfirm, BCReqConfirmSub  #########
			
			// ###########   Start : Add Table BCPurchaseOrder, BCPurchaseOrderSub  #########
			BCPurchaseOrderBean  po = setPurchaseOrderObject(form, request);			
			BCPurchaseOrderDAO dao3 = new BCPurchaseOrderDAO();
			String hql = "FROM BCPurchaseOrderBean WHERE docNo = '"+po.getDocNo()+"' ";
			List al = dao3.find(hql);
			if(al!=null && al.size()==0) {
				dao3.add(po);
			}else {
				String maxDocNo = CommonUtils.getMaxBCPurchaseOrderDocNo("PO1-");
				po.setDocNo(maxDocNo);
				dao3.add(po);
			}	
			// ###########   END : Add Table BCPurchaseOrder, BCPurchaseOrderSub  #########
			
		}else {//สำหรับการสร้าง PO จาก PO จะมีค่าเท่ากับการทำ add แบบ edit ของการสร้าง PO จาก PR
			System.out.println("add po แบบ ดึงจาก po");
			update(form, request,response);
		}
				
		return true;
	}	

	@SuppressWarnings("unchecked")
	public boolean update(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
				
//		// ###########   Start : Update Table BCReqConfirm, BCReqConfirmSub  #########		
//		BCReqConfirmBean reqConfirm = BCStkRequestBO.setReqConfirm(stkReq);
//		reqConfirm.setIsConfirm(0);
//		reqConfirm.setBillStatus(1);
//		reqConfirm.setIsCancel(0);
//		reqConfirm.setIttl_status("1");
//		reqConfirm.setIttl_userLevel((Integer)request.getSession().getAttribute("userLevel"));
//		
//		it =reqConfirm.getBcReqConfirmSubs().iterator();
//		while(it.hasNext()) {
//			BCReqConfirmSubBean sub = (BCReqConfirmSubBean)it.next();
//			sub.setIttl_status("1");
//		}								
//		BCReqConfirmDAO dao2 = new BCReqConfirmDAO();
//		dao2.update(reqConfirm.getDocNo(), reqConfirm);
//				
//		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCReqConfirmDAO #mode :update");
//		// ###########   END : Update Table BCReqConfirm, BCReqConfirmSub  #########
		
		// ###########   Start : Update Table BCPurchaseOrder, BCPurchaseOrderSub  #########
		BCPurchaseOrderBean  po = setPurchaseOrderObject(form, request);			
		BCPurchaseOrderDAO dao3 = new BCPurchaseOrderDAO();
//		System.out.println("po item size = "+po.getBcPurchaseOrderSubs().size());
		dao3.update(po.getDocNo(), po);
		
		// ###########   END : Add Table BCPurchaseOrder, BCPurchaseOrderSub  #########

		// ###########   Start : Update Table BCStkRequest, BCStkRequestSub  #########
		String rowOrderLineItem[] = new String[5]; 
		rowOrderLineItem[1] = request.getParameter("rowOrder1");
		rowOrderLineItem[2] = request.getParameter("rowOrder2");
		rowOrderLineItem[3] = request.getParameter("rowOrder3");
		rowOrderLineItem[4] = request.getParameter("rowOrder4");
		
		BCStkRequestForm reqForm = BCStkRequestBO.setForm(request);				
		BCStkRequestBean  stkReq = BCStkRequestBO.setRequestObject(reqForm);
		System.out.println("จำนวนที่ต้องอยู่ใน po= "+stkReq.getBcStkRequestSubs().size());
		
		Iterator it = stkReq.getBcStkRequestSubs().iterator();
		while (it.hasNext()) {
			BCStkRequestSubBean t = (BCStkRequestSubBean) it.next();
			
			switch (t.getLineNumber()) {
			case 1: t.setRowOrder(Integer.parseInt(rowOrderLineItem[1]));	break;
			case 2: t.setRowOrder(Integer.parseInt(rowOrderLineItem[2]));	break;
			case 3: t.setRowOrder(Integer.parseInt(rowOrderLineItem[3]));	break;
			case 4: t.setRowOrder(Integer.parseInt(rowOrderLineItem[4]));	break;

			default:
				break;
			}
		}
		
//		stkReq.setIsConfirm(1);
//		stkReq.setIsCancel(0);
//		stkReq.setBillStatus(1);
//		stkReq.setIttl_status("1");//ออกใบ PO แล้วจะเป็น 1
//		stkReq.setIttl_userLevel((Integer)request.getSession().getAttribute("userLevel"));
//		
//		request.setAttribute("docNoFind", reqForm.getDocNo());
//		
//		Iterator it = stkReq.getBcStkRequestSubs().iterator();
//		while(it.hasNext()) {
//			BCStkRequestSubBean sub = (BCStkRequestSubBean)it.next();
//			sub.setIsCancel(0);
//			sub.setIttl_status("1");
//			sub.setRowOrder(Integer.parseInt(reqForm.getRowOrder1()));
//		}						
		BCStkRequestDAO dao = new BCStkRequestDAO();
//		dao.update(stkReq.getDocNo(),stkReq);
		dao.updateStatusOfEditPO(stkReq, rowOrderLineItem, "1", po.getDocNo());//update item ที่ edit สร้าง po นั้นให้เป็น ittl_status = 1 , ตัว item ที่โดนลบออกในใบ po นี้ให้ ittl_status=0
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestDAO #mode :update");
		// ###########   END : Update Table BCStkRequest, BCStkRequestSub  #########		
		
		return true;
	}	

	@SuppressWarnings("unchecked")
	public boolean delete(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		
		String stkReqNo = ""+(String)request.getParameter("docNo");
		request.setAttribute("itemFind", stkReqNo);
		//หาดูก่อนว่า ใน bcpurchaseOrder มีใบไหนที่อ้าง พ.1 มากกว่า 1 ใบหรือไม่  (ถ้ามีใบเดียวก็ update status ด้วย แต่ถ้ามีมากกว่า 1 ไม่ต้อง update status)
		BCPurchaseOrderDAO dao3 = new BCPurchaseOrderDAO();
		String hql = "FROM BCPurchaseOrderSubShowBean WHERE stkReqNo='"+stkReqNo.trim()+"'";
		ArrayList al = (ArrayList)dao3.find(hql);
		
		if(al!=null && al.size()>0) {
			// ###########   Start : Update Table BCStkRequest, BCStkRequestSub  #########
			String rowOrderLineItem[] = new String[5]; 
			rowOrderLineItem[1] = request.getParameter("rowOrder1");
			rowOrderLineItem[2] = request.getParameter("rowOrder2");
			rowOrderLineItem[3] = request.getParameter("rowOrder3");
			rowOrderLineItem[4] = request.getParameter("rowOrder4");
			
			for (int i = 0; i < rowOrderLineItem.length; i++) {
				System.out.println("rowOrderLineItem["+i+"]="+rowOrderLineItem[i]);
			}
			
			BCStkRequestForm reqForm = BCStkRequestBO.setForm(request);				
			BCStkRequestBean  stkReq = BCStkRequestBO.setRequestObject(reqForm);
			
			Iterator it = stkReq.getBcStkRequestSubs().iterator();
			while (it.hasNext()) {
				BCStkRequestSubBean t = (BCStkRequestSubBean) it.next();
				
				switch (t.getLineNumber()) {
				case 1: t.setRowOrder(Integer.parseInt(rowOrderLineItem[1]));	break;
				case 2: t.setRowOrder(Integer.parseInt(rowOrderLineItem[2]));	break;
				case 3: t.setRowOrder(Integer.parseInt(rowOrderLineItem[3]));	break;
				case 4: t.setRowOrder(Integer.parseInt(rowOrderLineItem[4]));	break;

				default:
					break;
				}
			}			
			
//			stkReq.setIsConfirm(1);
//			stkReq.setIsCancel(0);
//			stkReq.setBillStatus(1);
//			stkReq.setIttl_status("0");//ยกเลิกออกใบ PO แล้วจะเป็น 0
//			stkReq.setIttl_userLevel((Integer)request.getSession().getAttribute("userLevel"));
//			
//			request.setAttribute("docNoFind", reqForm.getDocNo());
//			
//			Iterator it = stkReq.getBcStkRequestSubs().iterator();
//			while(it.hasNext()) {
//				BCStkRequestSubBean sub = (BCStkRequestSubBean)it.next();
//				sub.setIsCancel(0);
//				sub.setIttl_status("0");
//			}						
			BCStkRequestDAO dao = new BCStkRequestDAO();
			dao.updateStatusOfDeletePO(stkReq.getDocNo(), rowOrderLineItem, "0");//update item ที่ยกเลิกสร้าง po นั้นให้เป็น ittl_status = 0
			
			
			String user 	 = (String)request.getSession().getAttribute("userName");
			System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestDAO #mode :update");
			// ###########   END : Update Table BCStkRequest, BCStkRequestSub  #########
					
//			// ###########   Start : Update Table BCReqConfirm, BCReqConfirmSub  #########		
//			BCReqConfirmBean reqConfirm = BCStkRequestBO.setReqConfirm(stkReq);
//			reqConfirm.setIsConfirm(0);
//			reqConfirm.setBillStatus(0);
//			reqConfirm.setIsCancel(0);
//			reqConfirm.setIttl_status("0");
//			reqConfirm.setIttl_userLevel((Integer)request.getSession().getAttribute("userLevel"));
//			
//			it =reqConfirm.getBcReqConfirmSubs().iterator();
//			while(it.hasNext()) {
//				BCReqConfirmSubBean sub = (BCReqConfirmSubBean)it.next();
//				sub.setIttl_status("0");
//			}								
//			BCReqConfirmDAO dao2 = new BCReqConfirmDAO();
//			dao2.update(reqConfirm.getDocNo(), reqConfirm);
//					
//			System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCReqConfirmDAO #mode :update");
//			// ###########   END : Update Table BCReqConfirm, BCReqConfirmSub  #########		
		}		
				
		dao3.delete(request.getParameter("poDocNo"));
		
		return true;
	}
	
	@SuppressWarnings("unchecked")
	public boolean addOLD(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		//ถ้ามาจากหน้า : สร้าง PO จาก PO จะได้ค่า changePO= 'YES' ซึ่ง add ข้อมูลลง bcpurchaseorder,bcpurchaseorderSubเท่านั้น
		//ถ้ามาจากหน้า : สร้าง PO จากใบ พ1 :changePO=null
		String changePO = (String)request.getParameter("changePO");  
		
		// ถ้าเป็นการสร้าง PO จาก ใบ PO จะไม่ต้องกลับไป update ใน Table :BCStkRequest, BCStkRequestSub , BCReqConfirm, BCReqConfirmSub
		
		if(changePO==null || !changePO.equalsIgnoreCase("YES")) {
			// ###########   Start : Update Table BCStkRequest, BCStkRequestSub  #########
			BCStkRequestForm reqForm = BCStkRequestBO.setForm(request);				
			BCStkRequestBean  stkReq = BCStkRequestBO.setRequestObject(reqForm);
			stkReq.setIsConfirm(1);
			stkReq.setIsCancel(0);
			stkReq.setBillStatus(1);
			stkReq.setIttl_status("1");//ออกใบ PO แล้วจะเป็น 1
			stkReq.setIttl_userLevel((Integer)request.getSession().getAttribute("userLevel"));
			
			request.setAttribute("docNoFind", reqForm.getDocNo());
			
			Iterator it = stkReq.getBcStkRequestSubs().iterator();
			while(it.hasNext()) {
				BCStkRequestSubBean sub = (BCStkRequestSubBean)it.next();
				sub.setIsCancel(0);
				sub.setIttl_status("1");
			}						
			BCStkRequestDAO dao = new BCStkRequestDAO();
			dao.update(stkReq.getDocNo(),stkReq);
			
			String user 	 = (String)request.getSession().getAttribute("userName");
			System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestDAO #mode :update");
			// ###########   END : Update Table BCStkRequest, BCStkRequestSub  #########
					
			// ###########   Start : Update Table BCReqConfirm, BCReqConfirmSub  #########		
			BCReqConfirmBean reqConfirm = BCStkRequestBO.setReqConfirm(stkReq);
			reqConfirm.setIsConfirm(0);
			reqConfirm.setBillStatus(1);
			reqConfirm.setIsCancel(0);
			reqConfirm.setIttl_status("1");
			reqConfirm.setIttl_userLevel((Integer)request.getSession().getAttribute("userLevel"));
			
			it =reqConfirm.getBcReqConfirmSubs().iterator();
			while(it.hasNext()) {
				BCReqConfirmSubBean sub = (BCReqConfirmSubBean)it.next();
				sub.setIttl_status("1");
			}								
			BCReqConfirmDAO dao2 = new BCReqConfirmDAO();
			dao2.update(reqConfirm.getDocNo(), reqConfirm);
					
			System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCReqConfirmDAO #mode :update");
			// ###########   END : Update Table BCReqConfirm, BCReqConfirmSub  #########
		}
		
		// ###########   Start : Add Table BCPurchaseOrder, BCPurchaseOrderSub  #########
		BCPurchaseOrderBean  po = setPurchaseOrderObject(form, request);			
		BCPurchaseOrderDAO dao3 = new BCPurchaseOrderDAO();
		String hql = "FROM BCPurchaseOrderBean WHERE docNo = '"+po.getDocNo()+"' ";
		List al = dao3.find(hql);
		if(al!=null && al.size()==0) {
			dao3.add(po);
		}else {
			String maxDocNo = CommonUtils.getMaxBCPurchaseOrderDocNo("PO1-");
			po.setDocNo(maxDocNo);
			dao3.add(po);
		}	
		// ###########   END : Add Table BCPurchaseOrder, BCPurchaseOrderSub  #########
		
		return true;
	}

	
	@SuppressWarnings("unchecked")
	private BCPurchaseOrderBean setPurchaseOrderObject(ActionForm form, HttpServletRequest request)  {
		BCPurchaseOrderForm poForm = (BCPurchaseOrderForm)form;
		
		BCPurchaseOrderBean po = new BCPurchaseOrderBean();
		po.setIttl_purchaseCode(poForm.getIttl_purchaseCode());
		//System.out.println("ittl_purchaseOrder="+po.getIttl_purchaseCode());
		po.setTempMyDesc1("เลขที่ Purchase Code โรงงานดูในช่องหมายเหตุที่ 2");
		po.setTempMyDesc2(poForm.getIttl_purchaseCode());
		try {
			po.setTempMyDesc5(new String(poForm.getTempMyDesc5().getBytes("ISO8859_1"),"UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		po.setTempMyDesc3(poForm.getiTTL_deptRequest()+"     "+poForm.getDepartCode()+ "     "+poForm.getProjectCode());
		po.setTempMyDesc6("[M.TRANSIT]");
		try {
			po.setMyDescription(new String(poForm.getPoMyDescription().getBytes("ISO8859_1"),"UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		po.setDocNo(poForm.getPoDocNo());
		
//		System.out.println("po docNo="+po.getDocNo());
		
		po.setDocDate(CommonUtils.convertStringToDate(poForm.getPoDocDate())  );
//		System.out.println("po docDate = "+po.getDocDate());
		po.setDepartCode(poForm.getDepartCode());
		po.setApCode(poForm.getApCode());
		po.setCreatorCode(poForm.getPoCreatorCode());
		po.setProjectCode(poForm.getProjectCode());
		po.setCurrencyCode(poForm.getCurrencyCode());
		po.setSumOfItemAmount(poForm.getSumOfItemAmount());
		po.setDiscountAmount(poForm.getDiscountAmount());
		po.setAfterDiscount(poForm.getSumOfItemAmount()-poForm.getDiscountAmount());
		po.setBeforeTaxAmount(poForm.getSumOfItemAmount()-poForm.getDiscountAmount());
		po.setTaxAmount(poForm.getTaxAmount());
		po.setTotalAmount(poForm.getTotalAmount());
		po.setNetAmount(poForm.getNetAmount());
		po.setCurrencyCode(poForm.getCurrencyCode());
		
		po.setIsConfirm(1);
		
		po.setPoStatus(0);
		po.setBillStatus(0);
		po.setIsCancel(0);
		po.setIsCompleteSave(1);		
		po.setBillGroup("");		
		po.setCreateDateTime(new Date());
		
		HashSet hashSet = new HashSet();
		int index = 0;
		//System.out.println("################ get parameter itemcode i");
		for(int i=1; i<=4; i++) {
			
			if(request.getParameter("itemCode"+i) != null) {				
				BCPurchaseOrderSubBean sub = new BCPurchaseOrderSubBean();
				
				sub.setIttl_purchaseCode(po.getIttl_purchaseCode());
				if(request.getParameter("rowOrder"+i)!=null) {
					//System.out.println(request.getParameter("rowOrder"+i));
					sub.setIttl_rowOrderOfStkReq(Integer.valueOf(request.getParameter("rowOrder"+i)));
				}
				sub.setItemCode(request.getParameter("itemCode"+i));
				
//////////////////////////////////////////////////////				
				@SuppressWarnings("unused")
				BCItemBO itemBO = new BCItemBO();
				String hql = "FROM BCItemBean WHERE (code = '"+sub.getItemCode()+"' ) " +
				" and (code like '1-%' or code like '2-%' or code like '3-%' or code like '4-%' or " +
				"	   code like '5-%' or code like '6-%' or code like '7-%' )" +
				" ORDER BY code";
				BCReqConfirmDAO dao = new BCReqConfirmDAO();
				List results = dao.find(hql);
				if(results!=null && results.size()>0) {
					BCItemBean item = (BCItemBean)results.get(0);
					po.setTempMyDesc3("");					
					po.setTempMyDesc5("[STOCK]");
					po.setTempMyDesc6(""+item.getStockQty());
				}
//////////////////////////////////
				
				
				sub.setDocDate(po.getDocDate());
				sub.setApCode(po.getApCode());
				sub.setDepartCode(po.getDepartCode());
																
				sub.setWhCode("10");
				sub.setShelfCode("10");
				sub.setQty(Float.parseFloat(request.getParameter("qty"+i)));
				sub.setRemainQty(sub.getQty());
				sub.setPrice(Float.parseFloat(request.getParameter("price"+i)));
				sub.setAmount(Float.parseFloat(request.getParameter("amount"+i)));
				sub.setNetAmount(sub.getAmount());
				sub.setHomeAmount(sub.getAmount());
				sub.setUnitCode(request.getParameter("unitCode"+i));
				
				sub.setStkReqNo(request.getParameter("docNo"));
				sub.setConfirmNo(request.getParameter("docNo"));
				sub.setProjectCode(po.getProjectCode());
				sub.setCurrencyCode(po.getCurrencyCode());
				sub.setIsCancel(0);
				sub.setLineNumber(++index);
				sub.setCreatorCode(po.getCreatorCode());
				sub.setCreateDateTime(po.getCreateDateTime());				
				sub.setBcPurchaseOrder(po);
				
				sub.setTaxRate(7);
				sub.setPackingRate1(1);
				sub.setPackingRate2(1);
				
				try {
					sub.setItemName(new String(((String)request.getParameter("itemName"+i)).getBytes("ISO8859_1"),"UTF-8"));
					sub.setMyDescription(new String(((String)request.getParameter("myDescription"+i)).getBytes("ISO8859_1"),"UTF-8"));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				try {
					sub.setIttl_unitName(new String(((String)request.getParameter("unitName"+i)).getBytes("ISO8859_1"),"UTF-8"));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
				hashSet.add(sub);
			}						
		}
				
		po.setBcPurchaseOrderSubs(hashSet);
		
		return po;
	}
	@SuppressWarnings("unchecked")
	public ArrayList findAllOLD2(HttpServletRequest request) throws UnsupportedEncodingException  {
		
		@SuppressWarnings("unused")
		String userName = (String)request.getSession().getAttribute("userName");			
		String monthFind = (String)request.getParameter("monthFind");
		String yearFind = (String)request.getParameter("yearFind");
		String docNoFind = (String)request.getParameter("docNoFind");
				
		String hql = "";
		if(monthFind==null || yearFind==null) {			
			monthFind = CommonUtils.getCurrentDateFormat("MM");
			yearFind = CommonUtils.getCurrentDateFormat("yyyy");
									
			if (Integer.parseInt(yearFind)<2500) {
				yearFind = (""+Integer.parseInt(yearFind)+543).substring(2,2);
			}else {
				yearFind = yearFind.substring(2,2);
			}											
		}
		//Start : ค้นหา พ.1ขอซื้อทั้งหมด
		hql = "FROM BCReqConfirmSubShowBean s  " +
		 		"WHERE s.docNo like '%"+yearFind+monthFind+"%' "+		 		
		 		"and (s.docNo like 'F1%' or s.docNo like 'F2%') "+
		 		"ORDER BY s.docNo desc ";		
		
		if(docNoFind!=null) {
			docNoFind = new String(docNoFind.getBytes("ISO8859_1"),"UTF-8").trim();
			
			request.getSession().setAttribute("docNoFind", docNoFind);
			//request.setAttribute("docNoFind", docNoFind);
			
			hql = "FROM BCReqConfirmSubShowBean s  " +
						 "WHERE (s.docNo like '%"+docNoFind+"%' or s.itemName like '%"+docNoFind+"%') "+
						 "and (s.docNo like 'F1%' or s.docNo like 'F2%') "+
						 "ORDER BY s.docNo desc ";
		}		
		
		ArrayList results = new ArrayList();
		BCReqConfirmDAO dao = new BCReqConfirmDAO();
		ArrayList al = (ArrayList)dao.find(hql);
		
		if(al!=null && al.size()>0) {
			for (int i = 0; i < al.size(); i++) {
				BCReqConfirmSubShowBean bean = (BCReqConfirmSubShowBean)al.get(i);
								
				BCPurchaseOrderSubShow2Bean show = new BCPurchaseOrderSubShow2Bean();
				
				show.setStkReqNo(bean.getDocNo());									
				show.setItemCode(bean.getItemCode());
				show.setItemName(bean.getItemName());
				show.setQty(bean.getQty());				
				show.setIttl_unitName(bean.getIttl_unitName());																				
				show.setIttl_status(bean.getIttl_status());
				
				results.add(show);
				
			}
		}
		//End : ค้นหา พ.1ขอซื้อทั้งหมด
		//Start : หาว่า พ.1ขอซื้ออันไหนมี เลข PO บ้าง
		String stkDocNo = "";
		for(int i=0; i<results.size(); i++) {
			BCPurchaseOrderSubShow2Bean show = (BCPurchaseOrderSubShow2Bean)results.get(i);
			stkDocNo = stkDocNo +"'"+ show.getStkReqNo() + "',";
		}
		if(stkDocNo.length()>0) {
			stkDocNo = stkDocNo.substring(0, stkDocNo.length()-1);
		}else {
			stkDocNo = "''";
		}
		hql = " FROM BCPurchaseOrderSubShowBean s  " +
		 	  " WHERE  s.stkReqNo in ( "+stkDocNo+" )  ";		
		
		BCPurchaseOrderDAO dao2 = new BCPurchaseOrderDAO();
		ArrayList al2 = (ArrayList)dao2.find(hql);		
		
		@SuppressWarnings("unused")
		ArrayList al3 = new ArrayList();
		for (int i = 0; i < al2.size(); i++) {
			BCPurchaseOrderSubShowBean bean = (BCPurchaseOrderSubShowBean)al2.get(i);
//			boolean po_more = false;
			for (int j = 0; j < results.size(); j++) {
				BCPurchaseOrderSubShow2Bean bean2 = (BCPurchaseOrderSubShow2Bean)results.get(j);
				
				if(bean.getStkReqNo().equals(bean2.getStkReqNo())) {
					bean2.setDocNo(bean.getDocNo());
					bean2.setIttl_purchaseCode(bean.getIttl_purchaseCode());
					
					break;				
				}
			}			
		}
		//End : หาว่า พ.1ขอซื้ออันไหนมี เลข PO บ้าง
		
		return results;
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList findAllOLD(HttpServletRequest request) {
		
		@SuppressWarnings("unused")
		String userName = (String)request.getSession().getAttribute("userName");			
		String monthFind = (String)request.getParameter("monthFind");
		String yearFind = (String)request.getParameter("yearFind");
		String docNoFind = (String)request.getParameter("docNoFind");
				
		String hql = "";
		if(monthFind==null || yearFind==null) {
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("MM");			
			monthFind = sdf.format(date);
			
			sdf = new SimpleDateFormat("yyyy");
			String yy="";
			if (Integer.parseInt(sdf.format(date))<2500) {
				yy = (""+Integer.parseInt(sdf.format(date))+543).substring(2,2);
			}else {
				yy = sdf.format(date).substring(2,2);
			}			
			yearFind = yy;					
		}
		
		hql = "FROM BCReqConfirmBean s  " +
		 		"WHERE s.docNo like '%"+yearFind+monthFind+"%' "+
		 		"and s.isCancel =0 "+
		 		"and (s.docNo like 'F1%' or s.docNo like 'F2%') "+
		 		"ORDER BY s.docNo desc ";		
		
		if(docNoFind!=null) {
			request.getSession().setAttribute("docNoFind", docNoFind);
			
			hql = "FROM BCReqConfirmBean s  " +
						 "WHERE s.docNo like '%"+docNoFind+"%' "+
						 "and s.isCancel =0 "+
						 "and (s.docNo like 'F1%' or s.docNo like 'F2%') "+
						 "ORDER BY s.docNo desc ";
		}		
		
		BCReqConfirmDAO dao = new BCReqConfirmDAO();
		List results = dao.find(hql);

		if(results!=null) {			
			ArrayList<BCReqConfirmShowBean> al = new ArrayList<BCReqConfirmShowBean>();
			
			for(int i=0; i<results.size(); i++) {
				BCReqConfirmBean s2 = (BCReqConfirmBean)results.get(i);
				
				for(Object o: s2.getBcReqConfirmSubs()) {
					BCReqConfirmSubBean sub2 = (BCReqConfirmSubBean)o; 			
					
					BCReqConfirmShowBean bean = new BCReqConfirmShowBean();
					bean.setDocNo(s2.getDocNo());
					bean.setDocDate(s2.getDocDate());
					bean.setMyDescription(s2.getMyDescription());
					bean.setBillStatus(s2.getBillStatus());					
					bean.setItemCode(sub2.getItemCode());
					bean.setItemName(sub2.getItemName());					
					bean.setQty(sub2.getQty());					
					bean.setPrice(sub2.getPrice());
					bean.setAmount(sub2.getAmount());					
					bean.setIttl_unitName(sub2.getIttl_unitName());					
					bean.setIttl_status(s2.getIttl_status());
					bean.setMode("");
					
					al.add(bean);
				}				
			}
			results = al;
		}
		
		return (ArrayList)results;
	}

	@SuppressWarnings("unchecked")
	public Object findForm(HttpServletRequest request) {
				
		BCPurchaseOrderForm form = new BCPurchaseOrderForm();
		
		String docNo = (String)request.getParameter("docNo");
		System.out.println("#docNo :"+docNo);
		
		BCPurchaseOrderDAO dao = new BCPurchaseOrderDAO();	
		String hql = "FROM BCPurchaseOrderSubShowBean ";
				
		
		String poDocNo = (String)request.getParameter("docNo");
		if(poDocNo !=null) {			
			hql = hql + " WHERE docNo='"+poDocNo+"'";
		}
		
		List results = dao.find(hql);
		if (results!=null && results.size()>0) {
//			System.out.println("results.size()="+results.size());
			BCPurchaseOrderSubShowBean s2 = (BCPurchaseOrderSubShowBean)results.get(0);
			hql = "FROM BCPurchaseOrderBean WHERE docNo = '"+s2.getDocNo()+"' ";			
			
			results = dao.find(hql);
			if (results!=null && results.size()>0) {												
				BCPurchaseOrderBean s = (BCPurchaseOrderBean)results.get(0);
								
				form.setPoDocNo(s.getDocNo());
				form.setPoDocDate(CommonUtils.convertDateToString(s.getDocDate()));
				form.setPoCreatorCode(s.getCreatorCode());
				form.setPoMyDescription(s.getMyDescription());
				form.setIttl_purchaseCode(s.getIttl_purchaseCode());
				form.setTempMyDesc5(s.getTempMyDesc5());
				
			}			
		}	
		return form;
	}



	@SuppressWarnings("unchecked")
	public ArrayList findPrint(String hql) {
		BCPurchaseOrderDAO dao = new BCPurchaseOrderDAO();
		ArrayList results = (ArrayList)dao.find(hql);
		return results;	
	}

	public void setPage(HttpServletRequest request, String hql) {
		
		int pageNumber = 1, maxEntriesPerPage=10, lineNumber=1;		
		try {			
			pageNumber = Integer.parseInt(request.getParameter("page"));
		} catch (Exception e) {}
		
		if(request.getParameter("maxEntriesPerPage")!=null) {
			maxEntriesPerPage = Integer.parseInt(request.getParameter("maxEntriesPerPage"));			
		}
		if(request.getParameter("lineNumber")!=null) {
			request.setAttribute("lineNumber", request.getParameter("lineNumber"));
		}else {
			request.setAttribute("lineNumber", lineNumber);
		}
				
//		Page page = CommonUtils.getHibernatePage(hql, pageNumber, maxEntriesPerPage);				
		Page page = CommonUtils.getHibernateScrollableResultsPage(hql, pageNumber, maxEntriesPerPage);		
		
		request.setAttribute("maxEntriesPerPage", maxEntriesPerPage);
		request.setAttribute("allElement", page.getAllElement());
				
		if(page.getAllElement()>0) {			
			request.setAttribute("itemResults", page.getResultList() );
			request.setAttribute("offset", (pageNumber-1)*maxEntriesPerPage);
			request.setAttribute("currentPage", page.getCurrentPage());
			
			if(page.isNextPage()==true) {
				request.setAttribute("nextPage", page.getCurrentPage()+1);
			}
			
			if(page.isPreviousPage()==true) {
				request.setAttribute("previousPage", page.getCurrentPage()-1);
			}
			
			if(page.isFirstPage()==true) {
				request.setAttribute("firstPage", 1);
			}		
			
			if(page.isLastPage()==true && page.getMaxEntriesPerPage()!=0) {
				if(page.getAllElement()%page.getMaxEntriesPerPage()==0) {
					request.setAttribute("lastPage", page.getAllElement()/page.getMaxEntriesPerPage());	
				}else {
					request.setAttribute("lastPage", page.getAllElement()/page.getMaxEntriesPerPage()+1);
				}						
			}
		}
	}

	@SuppressWarnings("unchecked")
	public ArrayList find(ActionForm form, HttpServletRequest request) {
				
		POFindForm invForm =(POFindForm)form;
		String docNo = "";
		for(int i=0; i<invForm.getDocNoArray().length; i++) {
			docNo = docNo +"'"+ invForm.getDocNoArray()[i] + "',";
		}
		if(docNo.length()>0) {
			docNo = docNo.substring(0, docNo.length()-1);
		}
		
		String hql = "FROM BCPurchaseOrderBean s  " +
		 " WHERE  " +
		 " s.docNo in ( "+docNo+" )  ";		
		
		System.out.println("hql="+hql);
		BCPurchaseOrderDAO dao = new BCPurchaseOrderDAO();
		ArrayList results = (ArrayList)dao.find(hql);
		
		String stkReqNo = "";
		if(results!=null && results.size()>0) {
			BCPurchaseOrderBean poBean = (BCPurchaseOrderBean)results.get(0);

			Iterator it = poBean.getBcPurchaseOrderSubs().iterator();
			while(it.hasNext()) {
				BCPurchaseOrderSubBean sub = (BCPurchaseOrderSubBean)it.next();
				stkReqNo = sub.getStkReqNo();
			}			
		}

		BCStkRequestDAO dao2 = new BCStkRequestDAO();	
		hql = " FROM BCStkRequestBean h " +
		  	  " WHERE upper(h.docNo) = '"+stkReqNo+"' ";
				
		ArrayList results2 = (ArrayList)dao2.find(hql);
										
		return results2;
	}

	@SuppressWarnings("unchecked")
	public ArrayList displayPO(HttpServletRequest request) throws Exception {
					
		String monthFind = (String)request.getParameter("monthFind");
		String yearFind = (String)request.getParameter("yearFind");
		String docNoFind = ((String)request.getParameter("docNoFind"));
		String isDyeChem  =	(String)request.getParameter("isDyeChem");	
		String hql = "";				
		
		System.out.println("isDyeChem="+isDyeChem);
		
		if(monthFind==null || yearFind==null) {			
			monthFind = CommonUtils.getCurrentDateFormat("MM");
			yearFind = CommonUtils.getCurrentDateFormat("yyyy");
			
			if (Integer.parseInt(yearFind)<2500) {				
				yearFind = (""+(Integer.parseInt(yearFind)+543)).substring(2,4);								
			}else {
				yearFind = yearFind.substring(2,4);				
			}
		}
		request.setAttribute("monthFind", monthFind);
		request.setAttribute("yearFind", yearFind);		
		
		if(isDyeChem.equalsIgnoreCase("YES")) {			
			request.setAttribute("isDyeChem", "yes");
			request.setAttribute("titlePO", "ดูรายการใบ Purchase Order สีเคมี");
		}else {
			request.setAttribute("isDyeChem", "no");
			request.setAttribute("titlePO", "ดูรายการใบ Purchase Order ทั่วไป");
		}
		
		hql = "FROM BCPurchaseOrderBean s  " +
		 		"WHERE s.docNo like '%"+yearFind+monthFind+"%' "+							 				
		 		"ORDER BY s.docNo desc ";
		
		if(docNoFind!=null) {
			hql = "FROM BCPurchaseOrderBean s  " +
	 		"WHERE s.docNo like '%"+docNoFind.trim()+"%' or s.tempMyDesc2 like '%"+docNoFind.trim()+"%'"+
	 		" or s.ittl_purchaseCode like '"+docNoFind.trim()+"%' "+
	 		"ORDER BY s.docNo desc ";			
		}
		

		
		ArrayList results = new ArrayList();
		BCPurchaseOrderDAO dao = new BCPurchaseOrderDAO();
		ArrayList al = (ArrayList)dao.find(hql); 
				
		if(al!=null && al.size()>0) {
			for (int i = 0; i < al.size(); i++) {
				BCPurchaseOrderBean bean = (BCPurchaseOrderBean)al.get(i);
				Iterator it = bean.getBcPurchaseOrderSubs().iterator();
				
				while (it.hasNext()) {
					BCPurchaseOrderSubBean sub = (BCPurchaseOrderSubBean) it.next();
					
					BCPurchaseOrderSubShow2Bean show = new BCPurchaseOrderSubShow2Bean();
					if( isDyeChem.equalsIgnoreCase("YES") &&
						(sub.getItemCode().substring(0, 2).equals("2-") || sub.getItemCode().substring(0, 2).equals("3-"))  ) {												
						show.setDocNo(bean.getDocNo());
						show.setStkReqNo(sub.getStkReqNo());					
						show.setIttl_purchaseCode(bean.getIttl_purchaseCode());
						show.setItemCode(sub.getItemCode());
						show.setItemName(sub.getItemName());
						show.setQty(sub.getQty());
						show.setStkReqNo(sub.getStkReqNo());
						show.setIsCancel(sub.getIsCancel());
						show.setPoStatus(bean.getPoStatus());					
						
						ArrayList unitList = (ArrayList)request.getSession().getAttribute("unitCodeList");
						for (int j = 0; j < unitList.size(); j++) {
							BCItemUnitBean uBean = (BCItemUnitBean)unitList.get(j);
							if(sub.getUnitCode().equals(uBean.getCode())) {
								show.setIttl_unitName(uBean.getName());
								break;
							}						
						}					
						
						results.add(show);
						
					}else if( isDyeChem.equalsIgnoreCase("NO") &&
							    !sub.getItemCode().substring(0, 2).equals("2-") && 
							       !sub.getItemCode().substring(0, 2).equals("3-")  ) { 
					
						show.setDocNo(bean.getDocNo());
						show.setStkReqNo(sub.getStkReqNo());					
						show.setIttl_purchaseCode(bean.getIttl_purchaseCode());
						show.setItemCode(sub.getItemCode());
						show.setItemName(sub.getItemName());
						show.setQty(sub.getQty());
						show.setStkReqNo(sub.getStkReqNo());
						show.setIsCancel(sub.getIsCancel());
						show.setPoStatus(bean.getPoStatus());
						
						ArrayList unitList = (ArrayList)request.getSession().getAttribute("unitCodeList");
						for (int j = 0; j < unitList.size(); j++) {
							BCItemUnitBean uBean = (BCItemUnitBean)unitList.get(j);
							if(sub.getUnitCode().equals(uBean.getCode())) {
								show.setIttl_unitName(uBean.getName());
								break;
							}						
						}					
						
						results.add(show);						
					}				
				}
			}
		}
		
		return results; 
	}

	@SuppressWarnings("unchecked")
	public ArrayList findAll(HttpServletRequest request) throws Exception {

//		String userName = (String)request.getSession().getAttribute("userName");			
		String monthFind = (String)request.getParameter("monthFind");
		String yearFind = (String)request.getParameter("yearFind");
		String docNoFind = (String)request.getParameter("docNoFind");
				
		String hql = "";
		if(monthFind==null || yearFind==null) {			
			monthFind = CommonUtils.getCurrentDateFormat("MM");
			yearFind = CommonUtils.getCurrentDateFormat("yyyy");
									
			if (Integer.parseInt(yearFind)<2500) {
				yearFind = (""+Integer.parseInt(yearFind)+543).substring(2,2);
			}else {
				yearFind = yearFind.substring(2,2);
			}											
		}
		request.setAttribute("monthFind", monthFind);
		request.setAttribute("yearFind", yearFind);
		//Start : ค้นหา item ใน posub ก่อน ทั้งหมด
		hql = "FROM BCPurchaseOrderSubShowBean s  " +
		 		"WHERE s.stkReqNo like '%"+yearFind+monthFind+"%' "+		 		
		 		"and (s.stkReqNo like 'F1%' or s.stkReqNo like 'F2%') "+
		 		"and s.isCancel=0 "+
		 		"ORDER BY s.docNo desc ";		
		
		if(docNoFind!=null) {
			docNoFind = new String(docNoFind.getBytes("ISO8859_1"),"UTF-8").trim();
			
			request.getSession().setAttribute("docNoFind", docNoFind);
			//request.setAttribute("docNoFind", docNoFind);
			
			hql = "FROM BCPurchaseOrderSubShowBean s  " +
						 "WHERE (s.stkReqNo like '%"+docNoFind+"%' or s.itemName like '%"+docNoFind+"%' or s.ittl_purchaseCode like '%"+docNoFind+"%') "+
						 "and (s.stkReqNo like 'F1%' or s.stkReqNo like 'F2%') "+
						 "and s.isCancel=0 "+
						 "ORDER BY s.docNo desc ";
		}		
		
		ArrayList results = new ArrayList();
		BCPurchaseOrderDAO dao = new BCPurchaseOrderDAO();
		ArrayList al = (ArrayList)dao.find(hql);
		
		System.out.println(hql);
		System.out.println("size="+al.size());
		
		if(al!=null && al.size()>0) {
			for (int i = 0; i < al.size(); i++) {
				BCPurchaseOrderSubShowBean bean = (BCPurchaseOrderSubShowBean)al.get(i);
								
				BCPurchaseOrderSubShow2Bean show = new BCPurchaseOrderSubShow2Bean();									  																
								
				show.setDocNo(bean.getDocNo());
				show.setIttl_purchaseCode(bean.getIttl_purchaseCode());
				show.setStkReqNo(bean.getStkReqNo());									
				show.setItemCode(bean.getItemCode());
				show.setItemName(bean.getItemName());
				show.setQty(bean.getQty());				
				show.setIttl_unitName(bean.getIttl_unitName());																				
				
				show.setIttl_rowOrderOfStkReq(bean.getIttl_rowOrderOfStkReq());
																																														
				results.add(show);				
			}															
		}
		System.out.println("results.size="+results.size());
		//End : ค้นหา item ใน posub ก่อน ทั้งหมด
		
//		String stkDocNo = "";
//		for(int i=0; i<results.size(); i++) {
//			BCPurchaseOrderSubShow2Bean show = (BCPurchaseOrderSubShow2Bean)results.get(i);
//			stkDocNo = stkDocNo +"'"+ show.getStkReqNo() + "',";
//		}
//		if(stkDocNo.length()>0) {
//			stkDocNo = stkDocNo.substring(0, stkDocNo.length()-1);
//		}else {
//			stkDocNo = "''";
//		}
//		hql = " FROM BCReqConfirmSubShowBean s  " +
//		 	  " WHERE  s.stkRequestNo in ( "+stkDocNo+" )  ";		

//@@		private	String	ittl_status;  //สถานะการขอซื้อ
//@@		private	Integer	rowOrderOfStkReq;	//เอาไว้ check เพื่อ update ittl_status		

		hql = "FROM BCStkRequestSubShowBean s  " +
 		"WHERE s.docNo like '%"+yearFind+monthFind+"%' "+		 		
 		"and  (s.docNo like 'F1%' or s.docNo like 'F2%') "+
 		"and s.ittl_status!= -1 "+
 		"ORDER BY s.docNo desc ";		

		if(docNoFind!=null) {
			docNoFind = new String(docNoFind.getBytes("ISO8859_1"),"UTF-8").trim();
	
			hql = "FROM BCStkRequestSubShowBean s  " +
				 "WHERE (s.docNo like '%"+docNoFind+"%' or s.itemName like '%"+docNoFind+"%') "+
				 "and (s.docNo like 'F1%' or s.docNo like 'F2%') "+
				 "and s.ittl_status!= -1 "+
				 "ORDER BY s.docNo desc ";
		}		
		
		ArrayList al2 = (ArrayList)dao.find(hql);		
		
		System.out.println(hql);
		System.out.println("al2.size="+al2.size());
		
		@SuppressWarnings("unused")
		ArrayList al3 = new ArrayList();
		
		int temp_results = results.size();
		if(al2!=null && al2.size()>0) {
			for (int i = 0; i < al2.size(); i++) {
				BCStkRequestSubShowBean bean = (BCStkRequestSubShowBean)al2.get(i);
				
				boolean found_po = false;
				for (int j = 0; j < temp_results; j++) {																								
					BCPurchaseOrderSubShow2Bean show = (BCPurchaseOrderSubShow2Bean)results.get(j);
					
					// ถ้า rowOrder ของ BCStkRequestSub = ittl_rowOrderOfStkReq ของ BCPurchaseOrderSub
					// ให้เอาค่า ittl_status จาก BCStkRequestSub มา set 
					if(show.getIttl_rowOrderOfStkReq()!=null &&
							bean.getRowOrder().intValue()==show.getIttl_rowOrderOfStkReq().intValue()) {						
						
						show.setIttl_status(bean.getIttl_status());
						
						found_po = true;
						
				//		al3.add(show);
				//		break;						
					}																																											
				}				
				
				// ถ้า พ.1 ขอซื้อนั้นไม่ตรงกับใน BCPurchase ให้แสดงด้วย
				if (found_po==false) {
					
					BCPurchaseOrderSubShow2Bean show = new BCPurchaseOrderSubShow2Bean();					
					
					show.setStkReqNo(bean.getDocNo());									
					show.setItemCode(bean.getItemCode());
					show.setItemName(bean.getItemName());
					show.setQty(bean.getQty());				
					show.setIttl_unitName(bean.getIttl_unitName());					
					show.setIttl_status(bean.getIttl_status());																																	
					show.setIttl_rowOrderOfStkReq(bean.getRowOrder());
					
				//	al3.add(show);
					results.add(show);
				}
			}
		}		

		
		return results;//al3;//
	}

	@SuppressWarnings("unchecked")
	public BCStkRequestForm findStkRequestForm(HttpServletRequest request) {
		
		BCStkRequestForm form = new BCStkRequestForm();
		
		String docNo = ((String)request.getParameter("docNo")).trim();
		System.out.println("#docNo :"+docNo);
		
		BCPurchaseOrderDAO dao = new BCPurchaseOrderDAO();	
		String hql = "FROM BCPurchaseOrderBean h " +
				" WHERE upper(h.docNo) = '"+docNo.toUpperCase()+"' ";
				
		List results = dao.find(hql);
		if (results!=null && results.size()>0) {
			
			for(int i=0; i<results.size(); i++) {				
				BCPurchaseOrderBean s2 = (BCPurchaseOrderBean)results.get(i);
															
				form.setDepartCode(s2.getDepartCode());
				form.setProjectCode(s2.getProjectCode());
				form.setCreatorCode(s2.getCreatorCode());
																																								
				int id = 0;
				
				for(Object o: s2.getBcPurchaseOrderSubs()) {
					BCPurchaseOrderSubBean sub2 = (BCPurchaseOrderSubBean)o;
										
					if(id==0) {
						form.setDocNo(sub2.getStkReqNo());
						
						hql = "FROM BCStkRequestBean h " +
						" WHERE upper(h.docNo) = '"+sub2.getStkReqNo()+"' ";
						
						List al = dao.find(hql);
						if(al!=null && al.size()>0) {
							BCStkRequestBean aa = (BCStkRequestBean)al.get(0);
							form.setDocDate(""+aa.getDocDate());
							form.setiTTL_deptRequest(aa.getIttl_deptRequest());
							form.setMyDescription(aa.getMyDescription());
						}												
					}
					
//					switch (++id) {
					switch (sub2.getLineNumber().intValue()) {					
					case 1:	
						form.setRowOrder1(""+sub2.getIttl_rowOrderOfStkReq());
						form.setItemCode1(sub2.getItemCode());
						form.setItemName1(sub2.getItemName());						
						form.setUnitCode1(sub2.getUnitCode());
						form.setUnitName1(sub2.getIttl_unitName());
						System.out.println("form.getUnitName1()="+form.getUnitName1());
						form.setQty1(""+sub2.getQty());																				
						form.setIttl_unitName1(sub2.getIttl_unitName());
						form.setPrice1(""+sub2.getPrice());
						form.setAmount1(""+sub2.getAmount());
						form.setMyDescription1(sub2.getMyDescription());
						break;
					case 2:
						form.setRowOrder2(""+sub2.getIttl_rowOrderOfStkReq());
						form.setItemCode2(sub2.getItemCode());
						form.setItemName2(sub2.getItemName());						
						form.setUnitCode2(sub2.getUnitCode());
						form.setUnitName2(sub2.getIttl_unitName());
						form.setQty2(""+sub2.getQty());														
						form.setIttl_unitName2(sub2.getIttl_unitName());
						form.setPrice2(""+sub2.getPrice());
						form.setAmount2(""+sub2.getAmount());
						form.setMyDescription2(sub2.getMyDescription());
						break;
					case 3:
						form.setRowOrder3(""+sub2.getIttl_rowOrderOfStkReq());
						form.setItemCode3(sub2.getItemCode());
						form.setItemName3(sub2.getItemName());
						form.setUnitCode3(sub2.getUnitCode());
						form.setUnitName3(sub2.getIttl_unitName());
						form.setQty3(""+sub2.getQty());														
						form.setIttl_unitName3(sub2.getIttl_unitName());
						form.setPrice3(""+sub2.getPrice());
						form.setAmount3(""+sub2.getAmount());
						form.setMyDescription3(sub2.getMyDescription());
						break;		
					case 4:
						form.setRowOrder4(""+sub2.getIttl_rowOrderOfStkReq());
						form.setItemCode4(sub2.getItemCode());
						form.setItemName4(sub2.getItemName());
						form.setUnitCode4(sub2.getUnitCode());
						form.setUnitName4(sub2.getIttl_unitName());
						form.setQty4(""+sub2.getQty());														
						form.setIttl_unitName4(sub2.getIttl_unitName());
						form.setPrice4(""+sub2.getPrice());
						form.setAmount4(""+sub2.getAmount());
						form.setMyDescription4(sub2.getMyDescription());
						break;						
					}																																
				}				
			}			
		}	
		return form;
	}

	@SuppressWarnings("unchecked")
	public BCPurchaseOrderForm findPurchaseOrderForm(HttpServletRequest request) {

		BCPurchaseOrderForm form = new BCPurchaseOrderForm();
		
//		String docNo = ((String)request.getParameter("poDocNo"));
		String docNo = ((String)request.getParameter("docNo"));
		System.out.println("#docNo :"+docNo.trim());
		
		BCPurchaseOrderDAO dao = new BCPurchaseOrderDAO();	
		String hql = "FROM BCPurchaseOrderBean h " +
				" WHERE upper(h.docNo) = '"+docNo.toUpperCase()+"' ";
				
		List results = dao.find(hql);
		if (results!=null && results.size()>0) {
			
			for(int i=0; i<results.size(); i++) {				
				BCPurchaseOrderBean s2 = (BCPurchaseOrderBean)results.get(i);
				
//##################				
				form.setPoDocNo(s2.getDocNo());				
				form.setPoDocDate(CommonUtils.convertDateToString(s2.getDocDate()));				
				form.setPoMyDescription(s2.getMyDescription());
				form.setIttl_purchaseCode(s2.getIttl_purchaseCode());
				form.setTempMyDesc5(s2.getTempMyDesc5());				
//##################				
				form.setPoCreatorCode(s2.getCreatorCode());
				form.setDepartCode(s2.getDepartCode());
				form.setProjectCode(s2.getProjectCode());
																																											
				int id = 0;
				
				for(Object o: s2.getBcPurchaseOrderSubs()) {
					BCPurchaseOrderSubBean sub2 = (BCPurchaseOrderSubBean)o;
										
//					if(id==0) {
//						form.setDocNo(sub2.getStkReqNo());
//						
//						hql = "FROM BCStkRequestBean h " +
//						" WHERE upper(h.docNo) = '"+sub2.getStkReqNo()+"' ";
//						
//						List al = dao.find(hql);
//						if(al!=null && al.size()>0) {
//							BCStkRequestBean aa = (BCStkRequestBean)al.get(0);
//							form.setDocDate(""+aa.getDocDate());
//							form.setiTTL_deptRequest(aa.getIttl_deptRequest());
//							form.setMyDescription(aa.getMyDescription());
//						}												
//					}
					
					switch (++id) {
					case 1:	
						
						form.setItemCode1(sub2.getItemCode());
						form.setItemName1(sub2.getItemName());
						
						form.setUnitCode1(sub2.getUnitCode());
						
						form.setQty1(sub2.getQty());																										
						form.setPrice1(sub2.getPrice());
						form.setDiscountAmount1(sub2.getDiscountAmount());
						System.out.println(form.getDiscountAmount1());
						form.setAmount1(sub2.getAmount());
						form.setMyDescription1(sub2.getMyDescription());
						form.setUnitName1(sub2.getIttl_unitName());
						break;
					case 2:
						
						form.setItemCode2(sub2.getItemCode());
						form.setItemName2(sub2.getItemName());						
						form.setUnitCode2(sub2.getUnitCode());
						
						form.setQty2(sub2.getQty());																				
						form.setPrice2(sub2.getPrice());
						form.setAmount2(sub2.getAmount());
						form.setMyDescription2(sub2.getMyDescription());
						form.setUnitName2(sub2.getIttl_unitName());						
						break;
					case 3:
						
						form.setItemCode3(sub2.getItemCode());
						form.setItemName3(sub2.getItemName());
						form.setUnitCode3(sub2.getUnitCode());						
						form.setQty3(sub2.getQty());																				
						form.setPrice3(sub2.getPrice());
						form.setAmount3(sub2.getAmount());
						form.setMyDescription3(sub2.getMyDescription());
						form.setUnitName3(sub2.getIttl_unitName());						
						break;		
					case 4:
						
						form.setItemCode4(sub2.getItemCode());
						form.setItemName4(sub2.getItemName());
						form.setUnitCode4(sub2.getUnitCode());						
						form.setQty4(sub2.getQty());																				
						form.setPrice4(sub2.getPrice());
						form.setAmount4(sub2.getAmount());
						form.setMyDescription4(sub2.getMyDescription());
						form.setUnitName4(sub2.getIttl_unitName());						
						break;						
					}																																
				}				
			}			
		}	
		return form;
	}

	@SuppressWarnings("unchecked")
	public BCPurchaseOrderForm find2Form(HttpServletRequest request) {
		
		BCPurchaseOrderForm form = new BCPurchaseOrderForm();
		
		String docNo = (String)request.getParameter("docNo");
		System.out.println("#docNo :"+docNo);
		
		BCPurchaseOrderDAO dao = new BCPurchaseOrderDAO();	
		String hql = "FROM BCPurchaseOrderSubShowBean " ;
//				" WHERE stkReqNo = '"+docNo.toUpperCase()+"' ";
		
		String poDocNo = (String)request.getParameter("poDocNo");

		
		hql = "FROM BCPurchaseOrderBean WHERE docNo = '"+poDocNo+"' ";			
		
		List results = dao.find(hql);
		if (results!=null && results.size()>0) {												
			BCPurchaseOrderBean s = (BCPurchaseOrderBean)results.get(0);
							
			form.setPoDocNo(s.getDocNo());
			form.setPoDocDate(CommonUtils.convertDateToString(s.getDocDate()));
			form.setPoCreatorCode(s.getCreatorCode());
			form.setPoMyDescription(s.getMyDescription());
			form.setIttl_purchaseCode(s.getIttl_purchaseCode());
			form.setTempMyDesc5(s.getTempMyDesc5());			
		}			
			
		return form;
	}

	@SuppressWarnings("unchecked")
	public void showFind(HttpServletRequest request) throws UnsupportedEncodingException {
		@SuppressWarnings("unused")
		String userName = (String)request.getSession().getAttribute("userName");			
		String monthFind = (String)request.getParameter("monthFind");
		String yearFind = (String)request.getParameter("yearFind");

//		String docNoFind = (String)request.getParameter("itemFind");
//		String preDocNo  = (String)request.getSession().getAttribute("preDocNo")+"-PR";
//		int userLevel =  (Integer)request.getSession().getAttribute("userLevel");		

		String	itemFind = "";
		if(request.getParameter("itemFind")!=null) {
			itemFind = ((String)request.getParameter("itemFind"));
			itemFind = new String(itemFind.getBytes("ISO8859_1"),"UTF-8");
			request.setAttribute("itemFind", itemFind.trim());
			//request.getSession().setAttribute("itemFind", docNoFind.trim());
		}		
		
		if(monthFind==null) {			
			monthFind = CommonUtils.getCurrentDateFormat("MM");										
		}
		
		if(yearFind==null) {
			yearFind = CommonUtils.getCurrentDateFormat("yyyyy");
			
			if (Integer.parseInt(yearFind)<2500) {								
				yearFind = (""+(Integer.parseInt(yearFind)+543)).substring(2);			
			}else {
				yearFind = yearFind.substring(2);
			}			
		}
				
		request.getSession().setAttribute("monthFind", monthFind);
		request.getSession().setAttribute("yearFind", yearFind);
		
		String hql = "";
		if(itemFind==null||itemFind.trim().equals("")) {
			hql = "FROM BCStkRequestSubShowBean s  " +
//					 "WHERE upper(s.creatorCode)='"+userName.toUpperCase()+"' " +
//					 "and s.docNo like '"+preDocNo+"-"+yearFind+monthFind+"%' "+
//					 "and s.isCancel =0 "+
					 "ORDER BY s.docNo, lineNumber desc";			
		}else {
			hql = "FROM BCStkRequestSubShowBean s  " +

					 "WHERE s.docNo like '%"+itemFind.trim()+"%' "+
					 " or s.itemName like '%"+itemFind.trim()+"%'" +
//					 "and s.docNo like '"+preDocNo+"-%"+docNoFind.trim()+"%' "+
//					 "and s.isCancel =0 "+
					 "ORDER BY s.docNo, lineNumber desc";			
		}
		
		System.out.println(hql);
		Page.setPage(request, hql);
		
		hql = "FROM BCPurchaseOrderSubShowBean WHERE stkReqNo in (";
		ArrayList al = (ArrayList)request.getAttribute("itemResults");
		if(al!=null && al.size()>0) {
			Iterator it = al.iterator();
			while (it.hasNext()) {
				BCStkRequestSubShowBean reqSub = (BCStkRequestSubShowBean) it.next();
				hql += "'"+reqSub.getDocNo()+"',";
			}
			hql = hql.substring(0, hql.length()-1) +")";
			System.out.println("HQL="+hql);
			
			ArrayList al2 = (ArrayList)new BCPurchaseOrderDAO().find(hql);
			
			ArrayList al3 = new ArrayList();
			for (int i = 0; i < al.size(); i++) {
				BCStkRequestSubShowBean stkSub = (BCStkRequestSubShowBean)al.get(i);
				BCPurchaseOrderSubShowBean poSubShow = new BCPurchaseOrderSubShowBean();
							
				poSubShow.setStkReqNo(stkSub.getDocNo());
				poSubShow.setItemCode(stkSub.getItemCode());
				poSubShow.setItemName(stkSub.getItemName());
				poSubShow.setQty(stkSub.getQty());
				poSubShow.setIttl_unitName(stkSub.getIttl_unitName());
				poSubShow.setIttl_status(stkSub.getIttl_status());
							
				for (int j = 0; j < al2.size(); j++) {
					BCPurchaseOrderSubShowBean poSub = (BCPurchaseOrderSubShowBean)al2.get(j);
					
					//if(stkSub.getDocNo().equalsIgnoreCase(poSub.getStkReqNo())) {
					if(poSub.getIttl_rowOrderOfStkReq()!=null && 
							stkSub.getRowOrder().intValue()==poSub.getIttl_rowOrderOfStkReq().intValue()) {
						poSubShow.setDocNo(poSub.getDocNo());
					}
				}
				al3.add(poSubShow);
			}
			request.setAttribute("itemResults", al3);
		}		
	}

	@SuppressWarnings("unchecked")
	public boolean addpo(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		// ###########   Start : Add Table BCPurchaseOrder, BCPurchaseOrderSub  #########
		BCPurchaseOrderBean  po = setPurchaseOrderObject2(form, request);			
		BCPurchaseOrderDAO dao3 = new BCPurchaseOrderDAO();
		String hql = "FROM BCPurchaseOrderBean WHERE docNo = '"+po.getDocNo()+"' ";
		List al = dao3.find(hql);
		if(al!=null && al.size()==0) {
			dao3.add(po);
		}else {
			String maxDocNo = CommonUtils.getMaxBCPurchaseOrderDocNo("PO1-");
			po.setDocNo(maxDocNo);
			dao3.add(po);
		}
		request.setAttribute("docNoPrint", po.getDocNo());
		// ###########   END : Add Table BCPurchaseOrder, BCPurchaseOrderSub  #########		

		return true;
	}

	@SuppressWarnings("unchecked")
	public boolean addpo3(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		// ###########   Start : Add Table BCPurchaseOrder, BCPurchaseOrderSub  #########
		BCPurchaseOrderBean  po = setPurchaseOrderObject3(form, request);			
		BCPurchaseOrderDAO dao3 = new BCPurchaseOrderDAO();
		String hql = "FROM BCPurchaseOrderBean WHERE docNo = '"+po.getDocNo()+"' ";
		List al = dao3.find(hql);
		if(al!=null && al.size()==0) {
			System.out.println("add to db="+dao3.addPO(po));
		}else {
			String maxDocNo = CommonUtils.getMaxBCPurchaseOrderDocNo("PO1-");
			po.setDocNo(maxDocNo);
			System.out.println("add to db="+dao3.addPO(po));
		}
		request.setAttribute("docNoPrint", po.getDocNo());
		// ###########   END : Add Table BCPurchaseOrder, BCPurchaseOrderSub  #########		

		return true;
	}	
	
	@SuppressWarnings({ "unused", "unchecked" })
	private BCPurchaseOrderBean setPurchaseOrderObject2(ActionForm form,
			HttpServletRequest request) {
		BCPurchaseOrderForm poForm = (BCPurchaseOrderForm)form;
		
		BCPurchaseOrderBean po = new BCPurchaseOrderBean();
		po.setIttl_purchaseCode(poForm.getIttl_purchaseCode());
		//System.out.println("ittl_purchaseOrder="+po.getIttl_purchaseCode());
		po.setTempMyDesc1("เลขที่ Purchase Code โรงงานดูในช่องหมายเหตุที่ 2");
		po.setTempMyDesc2(poForm.getIttl_purchaseCode());
		
//		po.setTempMyDesc3(poForm.getiTTL_deptRequest()+"     "+poForm.getDepartCode()+ "     "+poForm.getProjectCode());
//		po.setTempMyDesc6("[M.TRANSIT]");
		try {
			po.setMyDescription(new String(poForm.getPoMyDescription().getBytes("ISO8859_1"),"UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		po.setDocNo(poForm.getPoDocNo());
		
//		System.out.println("po docNo="+po.getDocNo());
		
		po.setDocDate(CommonUtils.convertStringToDate(poForm.getPoDocDate())  );
//		System.out.println("po docDate = "+po.getDocDate());
		po.setDepartCode(poForm.getDepartCode());
		po.setApCode(poForm.getApCode());
		po.setCreatorCode(poForm.getPoCreatorCode());
		po.setProjectCode(poForm.getProjectCode());
		po.setCurrencyCode(poForm.getCurrencyCode());
		po.setSumOfItemAmount(poForm.getSumOfItemAmount());
		po.setDiscountAmount(poForm.getDiscountAmount());
		po.setAfterDiscount(poForm.getSumOfItemAmount()-poForm.getDiscountAmount());
		po.setBeforeTaxAmount(poForm.getSumOfItemAmount()-poForm.getDiscountAmount());
		po.setTaxAmount(poForm.getTaxAmount());
		po.setTotalAmount(poForm.getTotalAmount());
		po.setNetAmount(poForm.getNetAmount());
		po.setCurrencyCode(poForm.getCurrencyCode());
		
		po.setIsConfirm(1);
		
		po.setPoStatus(0);
		po.setBillStatus(0);
		po.setIsCancel(0);
		po.setIsCompleteSave(1);		
		po.setBillGroup("");		
		po.setCreateDateTime(new Date());
		
		HashSet hashSet = new HashSet();
		
		int index = 0;
		//System.out.println("################ get parameter itemcode i");
		for(int i=1; i<=4; i++) {
			
			if(request.getParameter("itemCode"+i) != null) {				
				BCPurchaseOrderSubBean sub = new BCPurchaseOrderSubBean();
				
				sub.setIttl_purchaseCode(po.getIttl_purchaseCode());
				sub.setItemCode(request.getParameter("itemCode"+i));
				
//////////////////////////////////////////////////////				
				BCItemBO itemBO = new BCItemBO();
				String hql = "FROM BCItemBean WHERE (code = '"+sub.getItemCode()+"' ) " +
				" and (code like '1-%' or code like '2-%' or code like '3-%' or code like '4-%' or " +
				"	   code like '5-%' or code like '6-%' or code like '7-%' )" +
				" ORDER BY code";
				BCReqConfirmDAO dao = new BCReqConfirmDAO();
				List results = dao.find(hql);
				if(results!=null && results.size()>0) {
					BCItemBean item = (BCItemBean)results.get(0);
					po.setTempMyDesc3("");					
					po.setTempMyDesc5("[STOCK]");
					po.setTempMyDesc6(""+item.getStockQty());
				}
//////////////////////////////////				
				sub.setDocDate(po.getDocDate());
				sub.setApCode(po.getApCode());
				sub.setDepartCode(po.getDepartCode());
																
				sub.setWhCode("10");
				sub.setShelfCode("10");
				sub.setQty(Float.parseFloat(request.getParameter("qty"+i)));
				sub.setRemainQty(sub.getQty());
				sub.setPrice(Float.parseFloat(request.getParameter("price"+i)));
				sub.setAmount(Float.parseFloat(request.getParameter("amount"+i)));
				sub.setNetAmount(sub.getAmount());
				sub.setHomeAmount(sub.getAmount());
				sub.setUnitCode(request.getParameter("unitCode"+i));
				
				sub.setProjectCode(po.getProjectCode());
				sub.setCurrencyCode(po.getCurrencyCode());
				sub.setIsCancel(0);
				sub.setLineNumber(++index);
				sub.setCreatorCode(po.getCreatorCode());
				sub.setCreateDateTime(po.getCreateDateTime());				
				sub.setBcPurchaseOrder(po);
				
				sub.setTaxRate(7);
				sub.setPackingRate1(1);
				sub.setPackingRate2(1);
				
				try {
					sub.setItemName(new String(((String)request.getParameter("itemName"+i)).getBytes("ISO8859_1"),"UTF-8"));
					sub.setMyDescription(new String(((String)request.getParameter("myDescription"+i)).getBytes("ISO8859_1"),"UTF-8"));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				try {
					sub.setIttl_unitName(new String(((String)request.getParameter("unitName"+i)).getBytes("ISO8859_1"),"UTF-8"));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
				hashSet.add(sub);
			}						
		}
				
		po.setBcPurchaseOrderSubs(hashSet);
		
		return po;
	}

	@SuppressWarnings({ "unused", "unchecked" })
	private BCPurchaseOrderBean setPurchaseOrderObject3(ActionForm form,
			HttpServletRequest request) {
		BCPurchaseOrderForm poForm = (BCPurchaseOrderForm)form;
		
		BCPurchaseOrderBean po = new BCPurchaseOrderBean();
		po.setIttl_purchaseCode(poForm.getTempMyDesc2());
		po.setTempMyDesc1("เลขที่ Purchase Code โรงงานดูในช่องหมายเหตุที่ 2");
		po.setTempMyDesc2(poForm.getTempMyDesc2());
				
		po.setTempMyDesc3(poForm.getTempMyDesc3());
		po.setTempMyDesc4(poForm.getTempMyDesc4());
		po.setTempMyDesc5(poForm.getTempMyDesc5());
		if(poForm.getTempMyDesc6()==null ||	poForm.getTempMyDesc6().equals("")) {
			po.setTempMyDesc6("[M.TRANSIT]");
		}else {
			po.setTempMyDesc6(poForm.getTempMyDesc6());
		}
		po.setTaxRate(poForm.getTaxRate());
		po.setExchangeRate(poForm.getExchangeRate());
		
		try {
			po.setMyDescription(new String(poForm.getMyDescription().getBytes("ISO8859_1"),"UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		
		try {
			po.setTempMyDesc7(new String(poForm.getApName().getBytes("ISO8859_1"),"UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}		
		
		po.setDocNo(poForm.getDocNo());		
		po.setDocDate(CommonUtils.convertStringToDate(poForm.getDocDate())  );
		po.setTaxType(poForm.getTaxType());
		po.setDepartCode(poForm.getDepartCode());
		po.setApCode(poForm.getApCode());
		
		po.setCreatorCode((String)request.getSession().getAttribute("userName"));
		po.setProjectCode(poForm.getProjectCode());
		po.setCurrencyCode(poForm.getCurrencyCode());
		
		po.setSumOfItemAmount(poForm.getSumOfItemAmount());
		po.setDiscountAmount(poForm.getDiscountAmount());
		po.setAfterDiscount(poForm.getSumOfItemAmount()-poForm.getDiscountAmount());
		po.setBeforeTaxAmount(poForm.getSumOfItemAmount()-poForm.getDiscountAmount());
		po.setTaxAmount(poForm.getTaxAmount());
		po.setTotalAmount(poForm.getTotalAmount());
		po.setNetAmount(poForm.getNetAmount());
		po.setCurrencyCode(poForm.getCurrencyCode());
		
		
		po.setIsConfirm(1);
		
		po.setPoStatus(0);
		po.setBillStatus(0);
		po.setIsCancel(0);
		po.setIsCompleteSave(1);		
		po.setBillGroup("");		
		po.setCreateDateTime(new Date());
		
		HashSet hashSet = new HashSet();
		
		int index = 0;
		//System.out.println("################ get parameter itemcode i");
		
		for(int i=0; i<poForm.getItemCode().length; i++) {
			
//			if(request.getParameter("itemCode"+i) != null) {
			if(poForm.getItemCode()[i] != null) {	
				BCPurchaseOrderSubBean sub = new BCPurchaseOrderSubBean();
				
				if(poForm.getIttl_rowOrderOfStkReq()[i]==null ||
						poForm.getIttl_rowOrderOfStkReq()[i].equals("")) {
					poForm.getIttl_rowOrderOfStkReq()[i] = "0";
				}
				sub.setIttl_rowOrderOfStkReq(Integer.parseInt(poForm.getIttl_rowOrderOfStkReq()[i]));
				sub.setIttl_purchaseCode(po.getIttl_purchaseCode());
				sub.setItemCode(poForm.getItemCode()[i]);
				
//////////////////////////////////////////////////////
				if(!po.getDocNo().contains("DC")) {
					BCItemBO itemBO = new BCItemBO();
					String hql = "FROM BCItemBean WHERE (code = '"+sub.getItemCode()+"' ) " +
					" and (code like '1-%' or code like '2-%' or code like '3-%' or code like '4-%' or " +
					"	   code like '5-%' or code like '6-%' or code like '7-%' )" +
					" ORDER BY code";
					BCReqConfirmDAO dao = new BCReqConfirmDAO();
					List results = dao.find(hql);
					if(results!=null && results.size()>0) {
						BCItemBean item = (BCItemBean)results.get(0);
						po.setTempMyDesc3("");
						if(po.getTempMyDesc5()==null) {
							po.setTempMyDesc5("[STOCK]");
						}
						po.setTempMyDesc6(""+item.getStockQty());
					}
				}
//////////////////////////////////				
				sub.setDocDate(po.getDocDate());
				sub.setApCode(po.getApCode());
				sub.setDepartCode(po.getDepartCode());
																
				sub.setWhCode("10");
				sub.setShelfCode("10");
				sub.setQty(Float.parseFloat(poForm.getQty()[i]));
				sub.setRemainQty(sub.getQty());
				sub.setPrice(Float.parseFloat(poForm.getPrice()[i]));
				
				if(CommonUtils.isNumberPercent(poForm.getLineItemDiscountAmount()[i])) {
					sub.setDiscountWord(poForm.getLineItemDiscountAmount()[i]);
					double percent1 = Double.valueOf( poForm.getLineItemDiscountAmount()[i].substring(0, poForm.getLineItemDiscountAmount()[i].length()-1) );
					
					DecimalFormat f = new DecimalFormat("#.00");
					float subDiscountAmount = Float.valueOf( f.format(sub.getQty()* sub.getPrice()*percent1/100));
					sub.setDiscountAmount(subDiscountAmount);
				}else {
					sub.setDiscountWord(poForm.getLineItemDiscountAmount() [i] );
					sub.setDiscountAmount(Float.valueOf(poForm.getLineItemDiscountAmount() [i] ));
				}
				
				sub.setAmount(Float.parseFloat(poForm.getAmount()[i]));
				sub.setNetAmount(sub.getAmount());
				sub.setHomeAmount(sub.getAmount());
				sub.setUnitCode(poForm.getUnitCode()[i]);
				
				sub.setProjectCode(po.getProjectCode());
				sub.setCurrencyCode(po.getCurrencyCode());
				sub.setIsCancel(0);
				sub.setLineNumber(++index);
				sub.setCreatorCode(po.getCreatorCode());
				sub.setCreateDateTime(po.getCreateDateTime());	
				
				sub.setBcPurchaseOrder(po);
				
				sub.setStkReqNo(poForm.getStkReqNo()[i]);
				sub.setConfirmNo(poForm.getConfirmNo()[i]);
				
				sub.setTaxRate(7);
				sub.setPackingRate1(1);
				sub.setPackingRate2(1);
				
				try {
					sub.setItemName(new String((poForm.getItemName()[i]).getBytes("ISO8859_1"),"UTF-8"));
					sub.setMyDescription(new String((poForm.getMyDescriptionItemName()[i]).getBytes("ISO8859_1"),"UTF-8"));
					
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				
				ArrayList unitList = (ArrayList)request.getSession().getAttribute("unitCodeList");
				for (int j = 0; j < unitList.size(); j++) {
					BCItemUnitBean uBean = (BCItemUnitBean)unitList.get(j);
					if(poForm.getUnitCode()[i].equals(uBean.getCode())) {
						sub.setIttl_unitName(uBean.getName());
						break;
					}						
				}
//				try {
//					sub.setIttl_unitName(new String(((String)request.getParameter("unitName"+i)).getBytes("ISO8859_1"),"UTF-8"));
//				} catch (UnsupportedEncodingException e) {
//					e.printStackTrace();
//				}	
				request.setAttribute("docNoP1", sub.getStkReqNo());
				hashSet.add(sub);
			}						
		}
				
		po.setBcPurchaseOrderSubs(hashSet);
		
		return po;
	}	
	
	public void displayPO2(HttpServletRequest request) throws UnsupportedEncodingException { 

		@SuppressWarnings("unused")
		String userName = (String)request.getSession().getAttribute("userName");			
		String monthFind = (String)request.getParameter("monthFind");
		String yearFind = (String)request.getParameter("yearFind");
//		String docNoFind = ((String)request.getParameter("docNoFind"));
		String isDyeChem  =	(String)request.getParameter("isDyeChem");		

		String	itemFind = "";
		if(request.getParameter("itemFind")!=null) {
			itemFind = ((String)request.getParameter("itemFind"));
			itemFind = new String(itemFind.getBytes("ISO8859_1"),"UTF-8");
			request.setAttribute("itemFind", itemFind.trim());
		}		
		
		String hql = "";
			
//=====================	
		
		if(monthFind==null || yearFind==null) {			
			monthFind = CommonUtils.getCurrentDateFormat("MM");
			yearFind = CommonUtils.getCurrentDateFormat("yyyy");
			
			if (Integer.parseInt(yearFind)<2500) {				
				yearFind = (""+(Integer.parseInt(yearFind)+543)).substring(2,4);								
			}else {
				yearFind = yearFind.substring(2,4);				
			}
		}
		request.setAttribute("monthFind", monthFind);
		request.setAttribute("yearFind", yearFind);		
				
//		hql = "FROM BCPurchaseOrderSubShowBean s  " +
//		 		"WHERE s.ittl_purchaseCode is not null ";							 				

		hql = "FROM BCPurchaseOrderBean " +
				"WHERE tempMyDesc2 is not null";
		
		if(itemFind!=null) {
//			hql = "FROM BCPurchaseOrderSubShowBean s  " +
//	 		"WHERE s.ittl_purchaseCode is not null and (s.docNo like '%"+itemFind.trim()+"%' "+
//	 		" or s.ittl_purchaseCode like '%"+itemFind.trim()+"%' ) ";//+

			hql = "FROM BCPurchaseOrderBean " +
	 				"WHERE tempMyDesc2 is not null and (docNo like '%"+itemFind.trim()+"%' "+
	 					" or tempMyDesc2 like '%"+itemFind.trim()+"%' ) ";			
		}
		
		if(isDyeChem.equalsIgnoreCase("YES")) {	
			hql = hql+" and docNo like 'PO1-DC%' ORDER BY docNo desc";
			
			request.setAttribute("isDyeChem", "yes");
			request.setAttribute("titlePO", "ดูรายการใบ Purchase Order สีเคมี");			
		}else {
			hql = hql+" and docNo like 'PO1%' " +
					"   and docNo not like 'PO1-DC%' " +
					" ORDER BY docNo desc";
			
			request.setAttribute("isDyeChem", "no");
			request.setAttribute("titlePO", "ดูรายการใบ Purchase Order ทั่วไป");
		}		
		
		System.out.println(hql);
		Page.setPage(request, hql);			
		//List list = (List)request.getAttribute("itemResults");
		//System.out.println(list.size());
	}

	public boolean updatepo(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		// ###########   Start : update Table BCPurchaseOrder, BCPurchaseOrderSub  #########
		BCPurchaseOrderBean  po = setPurchaseOrderObject2(form, request);			
		BCPurchaseOrderDAO dao = new BCPurchaseOrderDAO();		
		dao.updatepo(po.getDocNo(),po);
		
		request.setAttribute("docNoPrint", po.getDocNo());
		// ###########   END : update Table BCPurchaseOrder, BCPurchaseOrderSub  #########	
		return true;
	}

	public boolean deletepo(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		// ###########   Start : delete Table BCPurchaseOrder, BCPurchaseOrderSub  #########
		BCPurchaseOrderBean  po = setPurchaseOrderObject2(form, request);			
		BCPurchaseOrderDAO dao = new BCPurchaseOrderDAO();		
		dao.deletepo(po.getDocNo());
		
		request.setAttribute("itemFind", po.getDocNo());
		request.setAttribute("isDyeChem", request.getParameter("isDyeChem"));
		// ###########   END : delete Table BCPurchaseOrder, BCPurchaseOrderSub  #########	
		return true;
	}

	@SuppressWarnings("unchecked")
	public ArrayList findPR(ActionForm form, HttpServletRequest request) {
		
//		POFindForm invForm =(POFindForm)form;
		String docNo = request.getParameter("docNo");
				
		String hql = "FROM BCStkRequestBean " +
		 			 "WHERE docNo in ( '"+docNo+"' )  ";
		System.out.println(hql);
		
		ArrayList results = (ArrayList)new BCStkRequestDAO().find(hql);
				
		ArrayList al = new ArrayList();
		
		NumberFormat formatter = new DecimalFormat("0.00");
		@SuppressWarnings("unused")
		double amount=0;
		if(results !=null && results.size()>0) {
			
			int id=0;
			for(int i=0; i<results.size(); i++) {
				
				BCStkRequestBean p = (BCStkRequestBean)results.get(i);
				request.setAttribute("myDescription", p.getMyDescription());
				Iterator it = p.getBcStkRequestSubs().iterator();
				
				while (it.hasNext()) {
					
//					BCPurchaseOrderSubBean poSub = (BCPurchaseOrderSubBean) it.next();
					BCStkRequestSubBean pSub = (BCStkRequestSubBean)it.next();
					if(pSub.getRemainQty().floatValue()>0 &&
							pSub.getIttl_status().equals("0")) {
						BCPurchaseOrderShowBean bean = new BCPurchaseOrderShowBean();					
	//					if(pSub.getIttl_rowOrderOfStkReq()!=null) {
							bean.setIttl_rowOrderOfStkReq(pSub.getRowOrder());
	//					}else {
	//						bean.setIttl_rowOrderOfStkReq(0);
	//					}
						bean.setNumberForSort(""+pSub.getLineNumber());
						
	//					bean.setDocNo(poBean.getDocNo());
			        	bean.setLineNumber(""+(++id));
			        	bean.setItemCode(pSub.getItemCode());
			        	bean.setItemName(pSub.getItemName());
			        	
			        	bean.setLineItem_myDescription(pSub.getMyDescription());
			        	
			        	bean.setUnitCode(pSub.getUnitCode());
			        	bean.setQty(formatter.format(pSub.getQty()));
			        	
	//		        	bean.setIttl_purchaseCode(pSub.getIttl_purchaseCode());
			        	bean.setStkReqNo(p.getDocNo());		        	
			        	bean.setConfirmNo(p.getDocNo());		        			        			        	
			        	bean.setDepartCode(pSub.getDepartCode());
			        	bean.setProjectCode(pSub.getProjectCode());
			        	bean.setWhCode("10");
			        	bean.setShelfCode("10");
			        	
			        	bean.setLineItem_whCode("10");
			        	bean.setLineItem_shelfCode("10");
			        	bean.setLineItem_statusReceive("0");
			        	bean.setLineItem_departCode(pSub.getDepartCode());				        	
			        	bean.setLineItem_projectCode(pSub.getProjectCode());
	//		        	bean.setLineItem_poRefNo(poBean.getDocNo());
			        	
			        	bean.setPrice(""+pSub.getPrice());
	//		        	if(pSub.getDiscountWord()!=null) {
	//		        		bean.setLineItemDiscountAmount(""+poSub.getDiscountWord());	
	//		        	}else {
	//		        		bean.setLineItemDiscountAmount("0");
	//		        	}
			        
	//		        	bean.setAmount(""+poSub.getAmount());
			        	
	//		        	amount = amount + poSub.getAmount();
			        	
			        	request.setAttribute("departCode", bean.getDepartCode());
			        	request.setAttribute("projectCode", bean.getProjectCode().toUpperCase());
			        	System.out.println(bean.getProjectCode());
			        	System.out.println(bean.getProjectCode().length());
			        	al.add(bean);	
					}
				}
				if(p.getApCode()!=null) {
					request.setAttribute("apCode", p.getApCode());
				}else {
					request.setAttribute("apCode", "000");
					request.setAttribute("apName", "เจ้าหนี้ชั่วคราว");
				}
								
				request.setAttribute("sumOfItemAmount", "0.00");
				request.setAttribute("taxAmount", "0.00");
				request.setAttribute("totalAmount", "0.00");
				request.setAttribute("netAmount", "0.00");
				request.setAttribute("taxRate", "7.00");
				request.setAttribute("exchangeRate", "1.00");
				request.setAttribute("discountAmount", "0.00");
				request.setAttribute("exceptTaxAmount", "0.00");
				
				//end new
			}
		}	
// Start : เรียงลำดับ lineItem ตามลำดับการเลือก docNo;		
//		ArrayList alSort = new ArrayList();
//		for(int i=0; i<invForm.getDocNoArray().length; i++) {
//			docNo = docNo +"'"+ invForm.getDocNoArray()[i].trim() + "',";
//			
//			for(int j=0; j<al.size(); j++) {
//				BCPurchaseOrderShowBean po = (BCPurchaseOrderShowBean)al.get(j);
//				if(invForm.getDocNoArray()[i].trim().equals(po.getDocNo())) {
//					po.setNumberForSort(""+i+po.getNumberForSort());
//					alSort.add(po);
//				}
//			}			
//		}
// END : เรียงลำดับ lineItem ตามลำดับการเลือก docNo โดยใช้ ลำดับของ poDocNo ที่เลือกเข้ามา + lineNumber ของ itemใน po ใบนั้นๆ;
		
		Collections.sort(al);
		
		return al;
	}

	@SuppressWarnings("unchecked")
	public BCPurchaseOrderBean findPO(ActionForm form, HttpServletRequest request) {
		
		String docNo = request.getParameter("docNo");
		String hql   = "FROM BCPurchaseOrderBean " +
		 			   "WHERE docNo in ( '"+docNo+"' )  ";
		System.out.println(hql);
		
		ArrayList results = (ArrayList)new BCPurchaseOrderDAO().find(hql);
		BCPurchaseOrderBean poBean = new BCPurchaseOrderBean();
		
		if(results!=null && results.size()>0) {
			poBean = (BCPurchaseOrderBean)results.get(0); 
			//System.out.println("taxRate="+poBean.getTaxRate().floatValue());
		}
		
		return poBean;
	}

	public boolean update3(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		BCPurchaseOrderBean  po = setPurchaseOrderObject3(form, request);			
		BCPurchaseOrderDAO dao3 = new BCPurchaseOrderDAO();
		
		request.setAttribute("docNoPrint", po.getDocNo());
		return dao3.updatePO(po.getDocNo(), po);
	}

	public boolean delete3(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		BCPurchaseOrderBean  po = setPurchaseOrderObject3(form, request);			
		BCPurchaseOrderDAO dao3 = new BCPurchaseOrderDAO();
		if(po.getDocNo().contains("DC")) {
			request.setAttribute("docNoP1", null);
			request.setAttribute("itemFind", po.getDocNo());
		}
		return dao3.deletePO(po.getDocNo());
	}



	
}
